package org.szy.plugin.sys.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.mybatisflex.core.paginate.Page;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NoRepeatSubmit;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.param.CommonStatusParam;
import org.szy.core.common.entity.R;

import java.util.List;
import org.szy.plugin.sys.entity.SysRole;
import org.szy.plugin.sys.param.SysRoleQueryParam;
import org.szy.plugin.sys.service.SysRoleService;

import java.util.List;

/**
 * 系统角色控制器
 *
 * @author wangbuhao
 * @since 2023-06-26 20:06:27
 */
@Controller
@Mapping("/sys/role")
public class SysRoleController extends BaseController<SysRole> {

    private final String PATH_PREFIX = "sys/role/";
    @Inject
    private SysRoleService sysRoleService;

    /**
     * 系统角色列表页面
     */
    @Get
    @Mapping("/index")
    public ModelAndView index() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "index");
        return view;
    }

    /**
     * 系统角色编辑页
     */
    @Get
    @Mapping("/edit")
    public ModelAndView edit() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "edit");
        return view;
    }

    /**
     * 系统角色详情页
     */
    @Get
    @Mapping("/grantMenu")
    public ModelAndView grantMenu() {
        ModelAndView view = new ModelAndView(PATH_PREFIX + "grantMenu");
        return view;
    }

    /**
     * 获取系统角色分页数据
     *
     * @author wangbuhao
     * @since 2023-06-26 20:06:27
     */
    @Get
    @Mapping("/page")
    @SaCheckPermission(value = "sys_role_list", mode = SaMode.OR, orRole = "wangbuhao")
    public R<Page<SysRole>> page(SysRoleQueryParam param) {
        return R.data(sysRoleService.page(getPage(), param));
    }

    /**
     * 获取系统角色详情
     *
     * @author wangbuhao
     * @since 2023-06-26 20:06:27
     */
    @Get
    @Mapping("/getInfo")
    @SaCheckPermission(value = "sys_role_detail", mode = SaMode.OR, orRole = "wangbuhao")
    public R<SysRole> getInfo(@NotNull Long id) {
        return R.asNull(sysRoleService.getById(id));
    }

    /**
     * 添加系统角色
     *
     * @author wangbuhao
     * @since 2023-06-26 20:06:27
     */
    @Post
    @Mapping("/add")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_role_add", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> add(@Validated SysRole entity) {
        boolean save = sysRoleService.add(entity);
        return save ? R.ok("保存成功", entity.getId()) : R.fail("保存失败");
    }

    /**
     * 编辑系统角色
     *
     * @author wangbuhao
     * @since 2023-06-26 20:06:27
     */
    @Put
    @Mapping("/update")
    @NoRepeatSubmit
    @SaCheckPermission(value = "sys_role_update", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> update(@Validated SysRole entity) {
        boolean update = sysRoleService.edit(entity);
        return R.bool(update);
    }

    /**
     * 删除系统角色
     *
     * @author wangbuhao
     * @since 2023-06-26 20:06:27
     */
    @Delete
    @Mapping("/delete")
    @SaCheckPermission(value = "sys_role_delete", mode = SaMode.OR, orRole = "wangbuhao")
    public R<?> delete(@NotNull Long id) {
        boolean delete = sysRoleService.removeById(id);
        return R.bool(delete, "删除成功", "删除失败，数据可能正在使用或已删除");
    }

    /**
     * 修改系统角色状态
     *
     * @param param 公共状态参数
     * @return 修改结果
     */
    @Put
    @Mapping("/modifyStatus")
    public R<?> modifyStatus(CommonStatusParam param) {
        boolean status = sysRoleService.modifyStatus(param);
        return R.bool(status);
    }

}
